﻿// ThreadOperation.cpp: 实现文件
//

#include "stdafx.h"
#include "MesTest.h"
#include "ThreadOperation.h"
#include "MesTestDlg.h"
#include "MesGSoap.h"


// ThreadOperation

IMPLEMENT_DYNAMIC(ThreadOperation, CWnd)

ThreadOperation::ThreadOperation()
{

}

ThreadOperation::~ThreadOperation()
{
}


BEGIN_MESSAGE_MAP(ThreadOperation, CWnd)
END_MESSAGE_MAP()



// ThreadOperation 消息处理程序

void ThreadEx::ThreadSendMES(PVOID pVoid)
{
	Sleep(2000);
	int Type = 0;
	while (true)
	{
		bool bFlag = false;
		int RollChangeFlag(0);
		int ClipChangFlag(0);		
		//((CMesTestDlg*)g_pMainDialog)->OnBnClickedButton2();
		g_pPlc->DoSomething("GETCONNECTION", NULL, &bFlag);
		if (bFlag) 
		{			
			g_pPlc->DoSomething("READINT16", &g_PLCAddress.RollChange, &RollChangeFlag);
			if (RollChangeFlag == 1)
			{
				g_pPlc->DoSomething("READINT16", &g_PLCAddress.ProductType, &Type);
				CString cstr;
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.MaterialNum, &g_SendMes.cstrMaterialNum);
				g_SendMes.cstrMaterialNum.TrimRight();
				RollChangeFlag = 0;
				SendType = 3;
				g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.RollChange, &RollChangeFlag);
				if (g_SendMes.cstrMaterialNum == g_cstrFrontBatch)
				{
					int AlarmFlag = 1;
					g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.Alarm, &AlarmFlag);
					cstr.Format("前后卷号相同，请确认卷号是否正确 读取卷号为%s", g_SendMes.cstrMaterialNum);
					((CMesTestDlg*)g_pMainDialog)->WriteLog(cstr);
					((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstr);
				}
				else
				{
					g_cstrFrontBatch = g_SendMes.cstrMaterialNum;
					((CMesTestDlg*)g_pMainDialog)->SetsetMesAddrAndPort();
					((CMesTestDlg*)g_pMainDialog)->SendMES_QUIPMENT_DATA_COLLECT();
					SYSTEMTIME CurTime;
					GetLocalTime(&CurTime);
					if (Type == 1)
					{ 
						cstr.Format(_T("金相料,%04d/%02d/%02d-%02d/%02d/%02d,%s,\t\n"),
							CurTime.wYear, CurTime.wMonth, CurTime.wDay,
							CurTime.wHour, CurTime.wMinute, CurTime.wSecond, g_SendMes.cstrMaterialNum);
					}
					else
					{
						cstr.Format(_T("普通料,%04d/%02d/%02d-%02d/%02d/%02d,%s,\t\n"),
							CurTime.wYear, CurTime.wMonth, CurTime.wDay,
							CurTime.wHour, CurTime.wMinute, CurTime.wSecond, g_SendMes.cstrMaterialNum);
					}
				

					((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstr);
				}
				
				if (g_ProductCod == 1)
				{
					WritePrivateProfileString("EquipmentInfo", "ProductIden", g_SendMes.cstrMaterialNum, g_FilePathLog);
				}
				
			}
			g_pPlc->DoSomething("READINT16", &g_PLCAddress.ClipChange, &ClipChangFlag);
			if (ClipChangFlag == 3 || ClipChangFlag == 4)
			{
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.CutterNum, &g_SendMes.cstrCutterNum);
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.Box1, &g_SendMes.cstrBox1);
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.CreateTime, &g_SendMes.cstrCreateTime);
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.MaterialNum, &g_SendMes.cstrMaterialNum);
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.CutterFrequency, &g_SendMes.dCutterFrequency);
				g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.Tension, &g_SendMes.dTension);
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.OKCount, &g_SendMes.dOKCount);
				g_pPlc->DoSomething("READFLOAT32", &g_PLCAddress.FilmWidth, &g_SendMes.dFilmWidth);
				((CMesTestDlg*)g_pMainDialog)->ConvertDate(g_SendMes.cstrCreateTime);
				g_SendMes.cstrBox1.TrimRight();
				g_SendMes.cstrMaterialNum.TrimRight();
				ClipChangFlag = 0;
				SendType = 2;
				g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.ClipChange, &ClipChangFlag);
				((CMesTestDlg*)g_pMainDialog)->SetsetMesAddrAndPort();
				((CMesTestDlg*)g_pMainDialog)->SendMES_QUIPMENT_DATA_COLLECT();

				if (g_DanJiaCode == 1)
				{
					WritePrivateProfileString("EquipmentInfo", "DanJiaCode", g_SendMes.cstrBox1, g_FilePathLog);
				}
				if (g_ProductCod == 1)
				{
					WritePrivateProfileString("EquipmentInfo", "ProductIden", g_SendMes.cstrMaterialNum, g_FilePathLog);
				}
			}
			else if (ClipChangFlag == 1 || ClipChangFlag == 2)
			{
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.Box1, &g_SendMes.cstrBox1);
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.CreateTime, &g_SendMes.cstrCreateTime);
				g_SendMes.cstrBox1.TrimRight();
				((CMesTestDlg*)g_pMainDialog)->ConvertDate(g_SendMes.cstrCreateTime);
				ClipChangFlag = 0;
				SendType = 1;
				g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.ClipChange, &ClipChangFlag);
				((CMesTestDlg*)g_pMainDialog)->SetsetMesAddrAndPort();
				((CMesTestDlg*)g_pMainDialog)->SendMES_QUIPMENT_DATA_COLLECT(); 
				g_pPlc->DoSomething("READINT16", &g_PLCAddress.ProductType, &Type);
				SYSTEMTIME CurTime;
				GetLocalTime(&CurTime);
				CString cstr;
				if (Type == 1)
				{
					cstr.Format(_T("金相料,%04d/%02d/%02d-%02d/%02d/%02d,%s,%s,\t\n"),
						CurTime.wYear, CurTime.wMonth, CurTime.wDay,
						CurTime.wHour, CurTime.wMinute, CurTime.wSecond, g_cstrFrontBatch, g_SendMes.cstrBox1);
				}
				else
				{
					cstr.Format(_T("普通料,%04d/%02d/%02d-%02d/%02d/%02d,%s,%s,\t\n"),
						CurTime.wYear, CurTime.wMonth, CurTime.wDay,
						CurTime.wHour, CurTime.wMinute, CurTime.wSecond, g_cstrFrontBatch, g_SendMes.cstrBox1);
				}
				((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstr);

				if (g_DanJiaCode == 1)
				{
					WritePrivateProfileString("EquipmentInfo", "DanJiaCode", g_SendMes.cstrBox1, g_FilePathLog);
				}
			}
		}
		Sleep(200);
	}
	
}
void ThreadEx::ThreadRefreshStatus(PVOID pVoid)
{
	while (true)
	{
		Sleep(500);
		int nHeart = 1;
		g_pPlc->DoSomething("WRITEINT16", &g_PLCAddress.Heart, &nHeart);
	}

}
